package c_leetcode.a_160_intersection_tow_linked;

import java.util.HashSet;

import static c_leetcode.a_160_intersection_tow_linked.ListNode.printLiked;

/**
 * 哈希表
 * 时间复杂度    O(m+n)
 * 空间复杂度    O(m)
 *
 * @author He Kuntao
 * @since 2025/3/13 9:19
 */
public class HashTable {
    public static void main(String[] args) {
        ListNode[] listNode = ListNode.createListNode();
        printLiked(listNode[0], listNode[1]);
        System.out.println(new HashTable().getIntersectionNode(listNode[0], listNode[1]));

    }


    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        HashSet<ListNode> hashSet = new HashSet<>();
        while (headA != null) {
            hashSet.add(headA);
            headA = headA.next;
        }
        while (headB != null) {
            if (hashSet.contains(headB)) {
                return headB;
            }
            headB = headB.next;
        }
        return null;
    }


}
